<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-home"></i>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加属性模板</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="拼音" prop="pinyin">
        <el-input v-model="ruleForm.pinyin" placeholder="请输入父级类别的id，一级类别请填0"></el-input>
      </el-form-item>
      <el-form-item label="关键词列表" prop="keywords">
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "AddAttributeTemplateView",
  data() {
    return {
      ruleForm: {
        name: '测试属性模板',
        pinyin: 'pinyin',
        keywords: '',
        sort: '0',
      },
      rules: {
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 2, max: 12, message: '长度在 2 到 12 个字符', trigger: 'blur'}
        ],
        pinyin: [
          {required: true, message: '请输入拼音', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/attribute-templates/add-new'
          console.log(url)
          console.log('请求参数')
          console.log(this.ruleForm)
          this.axios
              .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
              .post(url, this.ruleForm).then(response => {
            let responseBody = response.data
            console.log(responseBody)
            if (responseBody.state == 20000) {
              this.$message.success("添加属性模板成功")
              this.resetForm(formName)
            } else {
              this.$message.error(responseBody.message)
            }

          })
        } else {
          console.log('error submit!!')
          return false
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>

</style>